import defaultRoute from "@/router/index"
const permission = {
    state: {
        routes: []
    },
    mutations: {
        SET_ROUTES: (state, userTypeId) => {
            state.routes = getRoutesByUserTypeId(defaultRoute, userTypeId)
        }
    }
}

// 通过不同的角色获取不同的路由权限
function getRoutesByUserTypeId(default_route, userId) {
    let hasMetaRoute = default_route.options.routes.filter(route => {
        return route.meta
    })
    
    let returnRoute = []
    hasMetaRoute.forEach((item, i) => {
        if(item.meta.auth.includes(userId)) {
            item.children = item.children.filter(item2 => {
                return item2.meta.auth.includes(userId);
            })
            returnRoute.push(item)
        }
    });
    return returnRoute;
}

export default permission